package com.cz.background.service.ey;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.cz.background.common.util.EyDataEncryptorHelper;
import com.cz.background.dto.ey.*;
import com.cz.background.entity.ey.EySchedule;
import com.cz.background.mapper.ey.EyScheduleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service

public class EyService {

    private final static Logger log = LoggerFactory.getLogger(EyService.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EyScheduleMapper eyScheduleMapper;

    public List<DoctorSchedule> getSourceInfo(ApiQueryScheduleDTO apiQueryScheduleDTO) {
        List<DoctorSchedule> doctorScheduleList = new ArrayList<>();
        String queryData = JSONUtil.toJsonStr(apiQueryScheduleDTO);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 设置请求类型为 JSON
        headers.add("Host", "pay.czey.com:8443");
        headers.add("encrypt", "4");
        headers.add("Referer", "https://servicewechat.com/wx8a6a76b77c6ef1ca/361/page-frame.html");
        headers.add("xweb_xhr", "1");
        headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11529");
        String queryEncryptData = EyDataEncryptorHelper.encrypt(queryData);
        EncryptDTO encryptDTO = new EncryptDTO();
        encryptDTO.setEncryptData(queryEncryptData);

        //log.info("queryEncryptData: {}", queryData);

        HttpEntity<String> entity = new HttpEntity<>(JSONUtil.toJsonStr(encryptDTO), headers);
        String url = "https://pay.czey.com:8443/apiV2/appointReg/getSourceInfo";

        ResponseEntity<Map> response =  restTemplate.exchange(url, org.springframework.http.HttpMethod.POST, entity, Map.class);
        Map<String, Object> result = response.getBody();
        //log.info("result: {}", result);
        // 发送 POST 请求
        if (MapUtil.isNotEmpty(result)) {
            String encryptData = MapUtil.getStr(result, "data");
            //log.info("encryptData: {}", encryptData);
            String decryptData = EyDataEncryptorHelper.decrypt(encryptData);
            //log.info("decryptData: {}", decryptData);

            doctorScheduleList = JSONUtil.toList(decryptData, DoctorSchedule.class);
        }
        return doctorScheduleList;
    }

    public void saveSchedule(CreateScheduleDTO createScheduleDTO) {
        EySchedule schedule = BeanUtil.copyProperties(createScheduleDTO, EySchedule.class);
        eyScheduleMapper.insert(schedule);
    }

    public List<EySchedule> querySchedule(QueryScheduleDTO queryScheduleDTO) {

        List<EySchedule> scheduleList = eyScheduleMapper.selectList(queryScheduleDTO);
        return scheduleList;
    }
    public void deleteSchedule(EnableScheduleDTO enableScheduleDTO) {
         eyScheduleMapper.deleteByDoctorCodeAndScheduleDate(enableScheduleDTO);
    }

    public void enableAndDisable(EnableScheduleDTO enableScheduleDTO) {
        EySchedule eySchedule = eyScheduleMapper.findByDoctorCodeAndScheduleDate(enableScheduleDTO);
        if(eySchedule.getStatus() == 1){
            eySchedule.setStatus(0);
        }else if(eySchedule.getStatus() == 0){
            eySchedule.setStatus(1);
        }
        eyScheduleMapper.updateStatusByDoctorCodeAndScheduleDate(eySchedule.getStatus()
                ,eySchedule.getDoctorCode(),eySchedule.getScheduleDate());
    }

    public List<DoctorDTO> queryDoctorList() {
        List<DoctorDTO> doctorList = new ArrayList<>();
        DoctorDTO doctorDTO_1127 = new DoctorDTO();
        doctorDTO_1127.setDoctorCode("1127");
        doctorDTO_1127.setDoctorName("王智婉");
        doctorList.add(doctorDTO_1127);

        DoctorDTO doctorDTO_1137 = new DoctorDTO();
        doctorDTO_1137.setDoctorCode("1137");
        doctorDTO_1137.setDoctorName("周健");
        doctorList.add(doctorDTO_1137);

        DoctorDTO doctorDTO_1133 = new DoctorDTO();
        doctorDTO_1133.setDoctorCode("1133");
        doctorDTO_1133.setDoctorName("徐凯虹");
        doctorList.add(doctorDTO_1133);

        DoctorDTO doctorDTO_1131 = new DoctorDTO();
        doctorDTO_1131.setDoctorCode("1131");
        doctorDTO_1131.setDoctorName("杨迎");
        doctorList.add(doctorDTO_1131);


        return doctorList;
    }
}
